<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad,changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,confirmModal,getDateStr} from '@/static/js/common.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				checkUserId:'',
				checkLastDate:'',
				checkDate:'' ,
				checkDateTitle:'',
				instId:'',
				userCacheKey:"per-zl-inspectionReport-title2-user-cache",
				eItemCacheKey:"per-zl-inspectionReport-title2-eItem-cache",
				itemCacheKey:"per-zl-inspectionReport-title2-item-cache",
				initValue:{
					maxScore:0,
					minScore:0,
					maxMoney: 0,
					minMoney:0
				}
			})
		},
		watch:{
			
		},
		methods:{
			onSelectActualEndDate(val){
				let _that = this
				if(moment(val).isAfter(_that.checkDate)){
					let title1 = _that.$refs.autoform.getEditorTitle("title2","actualEndDate")
					_that.om.toast(title1+'不能晚于'+_that.checkDateTitle)
					_that.$refs.autoform.clear("title2","actualEndDate")
				}
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						'dictSource/loadCombo',
						{
							sourceId:'283',
							categoryId:'5893'
						}
					)
				]
				Promise.all(arr).then(res=>{
						_that.$refs.autoform.changeEditor("title2","workTaskFrequencyId",'data',configSelectData(res[0],'id','text'))
						_that.$refs.autoform.initSelectIndex("title2","workTaskFrequencyId")
				})
				
			},
			clearNum(){
				let _that = this
				let maxScore = _that.initValue.maxScore 
				let minScore = _that.initValue.minScore 
				let maxMoney = _that.initValue.maxMoney 
				let minMoney = _that.initValue.minMoney
				_that.$refs.autoform.changeEditor("title2","score",'prompt',minScore +'≤'+'x'+'≤'+ maxScore)
				_that.$refs.autoform.changeEditor("title2","money",'prompt',minMoney+'≤'+'x'+'≤'+maxMoney)
				_that.$refs.autoform.clear("title2","score")
				_that.$refs.autoform.clear("title2","money")
				_that.$refs.autoform.clear("title2","num")
			},
			onInputNum(val){
				let _that = this
				if(Number(val)){
					_that.$refs.autoform.clear("title2","score")
					_that.$refs.autoform.clear("title2","money")
					let maxScore = _that.initValue.maxScore * Number(val)
					let minScore = _that.initValue.minScore * Number(val)
					let maxMoney = _that.initValue.maxMoney * Number(val)
					let minMoney = _that.initValue.minMoney * Number(val)
					_that.$refs.autoform.changeEditor("title2","score",'prompt',minScore +'≤'+'x'+'≤'+ maxScore)
					_that.$refs.autoform.changeEditor("title2","money",'prompt',minMoney+'≤'+'x'+'≤'+maxMoney)
				}
			},
			getFirstLastDate(id){
				let _that = this
				_that.om.request(
					"inspectionReport/getFirstLastDate",
					{
						itemId: id,
						checkDate: _that.checkDate
					}
				).then(res=>{
					if(res.succeed){
						_that.$refs.autoform.formdata.firstDate = res.value.startTime
						_that.$refs.autoform.formdata.lastDate = res.value.endTime 
					}
				})
			},
			getObj(id){
				let _that = this
				_that.om.request(
					"behaviorEvaluationItemShow/getObj",
					{
						id: id,
					}
				).then(res=>{
					if(res.succeed){
						_that.$refs.autoform.formdata.checkRemark = res.value.checkRemark
					}
				})
			},
			onInputMoney(newValue){
				let _that = this
				if(newValue){
					let minMoney = Number(_that.initValue.minMoney)
					let maxMoney =  Number(_that.initValue.maxMoney)
					if(_that.$refs.autoform.formdata.num){
						minMoney = minMoney * Number(_that.$refs.autoform.formdata.num)
						maxMoney = maxMoney * Number(_that.$refs.autoform.formdata.num)
					}
					if(Number(newValue)< minMoney){
						_that.$refs.autoform.formdata.money = minMoney
					}
					if(Number(newValue)>maxMoney){
						_that.$refs.autoform.formdata.money = maxMoney
					}
				}else{
					_that.$refs.autoform.clear("title2","money")
				}
			},
			onInputScore(newValue){
				let _that = this
				if(newValue){
					let minScore = Number(_that.initValue.minScore)
					let maxScore =  Number(_that.initValue.maxScore)
					if(_that.$refs.autoform.formdata.num){
						minScore = minScore * Number(_that.$refs.autoform.formdata.num)
						maxScore = maxScore * Number(_that.$refs.autoform.formdata.num)
					}
					if(Number(newValue)<minScore){
						_that.$refs.autoform.formdata.score = minScore
					}
					if(Number(newValue)>maxScore){
						_that.$refs.autoform.formdata.score = maxScore
					}
				}else{
					_that.$refs.autoform.clear("title2","score")
				}
			},
			clearPos(){
				let _that = this
				_that.$refs.autoform.clear("title2","postId")
				_that.$refs.autoform.clear("title2","parentUserName")
				_that.$refs.autoform.clear("title2","instName")
			},
			clearItem(){
				let _that = this
				_that.$refs.autoform.clear("title2","firstDate")
				_that.$refs.autoform.clear("title2","lastDate")
				_that.$refs.autoform.clear("title2","itemId")
				_that.$refs.autoform.clear("title2","targetValue")
				_that.$refs.autoform.clear("title2","planEndDate")
				_that.$refs.autoform.clear("title2","inspectionRemark")
				_that.$refs.autoform.clear("title2","evaluateId")
				_that.$refs.autoform.clear("title2","checkFrequencyId")
				_that.$refs.autoform.clear("title2","num")
				_that.$refs.autoform.clear("title2","score")
				_that.$refs.autoform.clear("title2","money")
				_that.$refs.autoform.clear("title2","endDate")
				_that.$refs.autoform.clear("title2","checkRemark")
				_that.$refs.autoform.clear("title2","remark")
			},
			clearUser(){
				let _that = this
				confirmModal(_that,'提示信息',"您确认删除所选内容?",()=>{
					_that.$refs.autoform.clearGroup("title2")
				},()=>{
					
				})
				
			},
			verifyUser(){
				let _that = this
				if(_that.$refs.autoform.formdata.monthEvaluationItemShowId){
					return false
				}else{
					return true
				}
			},
			clearCode(){
				let _that = this
				_that.$refs.autoform.clearGroup("title2")
			},
			getFlagByEvaluateId(id){
				let _that = this
				_that.om.request(
					"inspectionReport/getFlagByEvaluateId",
					{
						evaluateId: id
					}
				).then(res=>{
					if(res.value){
						if(res.value.moneyDisabledFlag){
							_that.$refs.autoform.changeEditor("title2","money","type","labelbox")
						}else{
							_that.$refs.autoform.changeEditor("title2","money","type","numberbox")
						}
						if(res.value.scoreDisabledFlag){
							_that.$refs.autoform.changeEditor("title2","score","type","labelbox")
						}else{
							_that.$refs.autoform.changeEditor("title2","score","type","numberbox")
						}
					}
				})
			},
			onSelectPos(item){
				let _that = this
				_that.$refs.autoform.formdata.instId = item.instId
				_that.$refs.autoform.formdata.instName = item.instName
				_that.getParentUser(item.id)
			},
			getParentUser(id){
				let _that = this
				_that.om.request(
					"inspectionReport/getParentUser",
					{
						postId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.parentUserId = res.value.id
					_that.$refs.autoform.formdata.parentUserName = res.value.username
				})
			},
			findPostList(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosComboboxByUser",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title2","postId","data",configSelectData(res,"id","posname"))
					if(!_that.$refs.autoform.formdata.postId){
						_that.$refs.autoform.initSelectVal("title2","postId")
					}else{
						_that.$refs.autoform.initSelectIndex("title2","postId")
					}
				})
			}
		},
		onShow() {
			let _that  = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.userId = d.id
							formdata.userName = d.username
							_that.$refs.autoform.changeEditor("title2","postId","type","selectbox")
							_that.$refs.autoform.changeEditor("title2","itemId","type","linkbox")
							_that.findPostList(d.id)
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.eItemCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							Object.assign(formdata,d)
							_that.findPostList(_that.$refs.autoform.formdata.userId)
							_that.onSelectPos({id:_that.$refs.autoform.formdata.postId,
								instId:_that.$refs.autoform.formdata.instId,
								instName:_that.$refs.autoform.formdata.instName
							})
							_that.getFlagByEvaluateId(_that.$refs.autoform.formdata.evaluateId)
							_that.$refs.autoform.changeEditor("title2","postId","type","labelbox")
							_that.$refs.autoform.changeEditor("title2","itemId","type","labelbox")
							uni.removeStorage({
								key:_that.eItemCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.itemCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.itemId = d.itemId
							formdata.itemName = d.itemName
							_that.$refs.autoform.clear("title2",'monthEvaluationItemShowId')
							formdata.evaluateId = d.evaluateId
							formdata.evaluateName = d.evaluateName
							formdata.checkFrequencyId = d.checkFrequencyId
							formdata.checkFrequencyName = d.checkFrequencyName
							// formdata.checkRemark = d.checkRemark
							formdata.remark = d.remark
							formdata.endDate = _that.checkDate
							formdata.num = 0
							_that.$refs.autoform.clear("title2",'money')
							_that.$refs.autoform.clear("title2",'num')
							_that.$refs.autoform.clear("title2",'score')
							_that.initValue.maxScore = d.maxScore
							_that.initValue.minScore = d.minScore
							_that.initValue.maxMoney = d.maxMoney
							_that.initValue.minMoney = d.minMoney
							let maxScore = d.maxScore
							let minScore = d.minScore
							let maxMoney = d.maxMoney
							let minMoney = d.minMoney
							if(formdata.num){
								maxScore = maxScore * formdata.num
								minScore = minScore * formdata.num
								maxMoney = maxMoney * formdata.num
								minMoney = minMoney * formdata.num
							}
							_that.$refs.autoform.changeEditor("title2","score",'prompt',minScore+'≤'+'x'+'≤'+maxScore)
							_that.$refs.autoform.changeEditor("title2","money",'prompt',minMoney+'≤'+'x'+'≤'+maxMoney)
							_that.getFlagByEvaluateId(d.evaluateId)
							_that.getFirstLastDate(d.itemId)
							_that.getObj(d.itemId)
							uni.removeStorage({
								key:_that.itemCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.checkUserId){
					_that.checkUserId = e.checkUserId
				}
				if(e.checkLastDate){
					_that.checkLastDate = e.checkLastDate
				}
				if(e.checkDate){
					_that.checkDate = e.checkDate
				}
				if(e.checkDateTitle){
					_that.checkDateTitle = e.checkDateTitle
				}
				if(e.instId){
					_that.instId = e.instId
				}
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor('title2', 'userId', {
								link: "../../public/pageSearch/pageSearch",
								"linkParam": {
									other: JSON.stringify({
										instId: _that.instId
									}),
									valuefield: "id",
									searchfield: 'q',
									initload: true,
									namefield: "username",
									notefield:"sexName",
									extrafield:"subMobile",
									url: "orgInstPosUserOfficial/findUserPagerByInst",
									cachename: _that.userCacheKey,
									pagination: true,
									placeholder: "请输入姓名"
								}
							}, null, _that.autoformdata)
							changeEditor(_that.autoformdata[0].editors,"monthEvaluationItemShowId",{
								link:"../../public/search/search",
								"linkParam":{
									other:JSON.stringify({
										checkUserId: _that.checkUserId,
										checkLastDate: _that.checkLastDate,
										checkDate: _that.checkDate,
										acceptCheckInstId: _that.instId,
										filterIdListStr: [],
										formId: '',
										oldValue: '',
									}),
									url:"inspectionReport/getMonthEvaluationItemShowList",
									namefield:'itemName',
									notefield:'monthEvaluationItemShowCode',
									initload:true,
									cachename:_that.eItemCacheKey,
								}
							})
							
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "47")
							if(_that.formdata.fileList) {
								var tempFileList = []
								var fileUrlList = _that.formdata.fileList.map(t=>{
									tempFileList.push(t.fileSaveName)
									return t.fileSaveName
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
								changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
							}
							changeEditor("title2","itemId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
									}),
									url:"inspectionReport/getItemList",
									namefield:"itemName",
									initload:true,
									cachename:_that.itemCacheKey
								}
							},null,_that.autoformdata)
							
								if(_that.formdata.maxMoney && (_that.formdata.minMoney + '')){
									let minMoney = _that.formdata.minMoney
									let maxMoney = _that.formdata.maxMoney
									if(_that.formdata.num){
										minMoney = minMoney * Number(_that.formdata.num) 
										maxMoney = maxMoney * Number(_that.formdata.num) 
									}
									changeEditor("title2","money",'prompt',minMoney+'≤'+'x'+'≤'+maxMoney,_that.autoformdata)
								}else{
									changeEditor("title2","money",'type','labelbox',_that.autoformdata)
								}
								
								_that.initload()
								
								if(_that.formdata.maxScore && (_that.formdata.maxScore + '')){
									let minScore = _that.formdata.minScore
									let maxScore = _that.formdata.maxScore
									if(_that.formdata.num){
										minScore = minScore * Number(_that.formdata.num) 
										maxScore = maxScore * Number(_that.formdata.num) 
									}
									changeEditor("title2","score",'prompt',minScore+'≤'+'x'+'≤'+maxScore,_that.autoformdata)
								}
							}
							_that.$nextTick(function(){
								if(_that.$refs.autoform.formdata.userId){
									_that.findPostList(_that.$refs.autoform.formdata.userId)
								}
								if(!_that.$refs.autoform.formdata.endDate){
									_that.$refs.autoform.formdata.endDate = _that.checkDate
								}
								if(_that.$refs.autoform.formdata.monthEvaluationItemShowId){
									_that.$refs.autoform.changeEditor("title2","postId","type","labelbox")
									_that.$refs.autoform.changeEditor("title2","itemId","type","labelbox")
								}
							})
						}
					})
				}
							
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
